import 'dart:async';

import 'package:flutter/material.dart';
import 'package:hotpot/widgets/widget_ext.dart';

class TimeBox extends StatefulWidget {
  const TimeBox({super.key});

  @override
  State<TimeBox> createState() => _TimeBoxState();
}

class _TimeBoxState extends State<TimeBox> {
  late String time, week, date;

  Timer? timer;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    loadDate();
  }

  @override
  Widget build(BuildContext context) {
    return Row(
      children: [
        time.text(size: 16, color: Colors.white, bold: true).sizedBox(width: 80),
        // 2.wSizedBox,
        Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            week.text(size: 7, color: Colors.white, bold: true),
            date.text(size: 7, color: Colors.white, bold: true),
          ],
        ),
      ],
    );
  }

  void loadDate() {
    var now = DateTime.now();
    time = '${now.hour.value}:${now.minute.value}:${now.second.value}';
    week = '周${now.weekday.week}';
    date = '${now.day.value}-${now.month.value}-${now.year.value}';
    timer = Timer(const Duration(seconds: 1), loadDate);
    setState(() {});
  }

  @override
  void dispose() {
    // TODO: implement dispose
    timer?.cancel();
    super.dispose();
  }
}

extension _NumExt on int {
  String get value => this < 10 ? '0$this' : '$this';

  String get week {
    switch (this) {
      case 1:
        return '一';
      case 2:
        return '二';
      case 3:
        return '三';
      case 4:
        return '四';
      case 5:
        return '五';
      case 6:
        return '六';
    }
    return '日';
  }
}
